﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using Utils;

namespace BookM
{
    public partial class AmendPage : Form
    {
        private string bookid;
        private int id;
        public AmendPage(string bookidID, int ID)
        {
            bookid = bookidID;
            id = ID;
            InitializeComponent();
        }
        public AmendPage()
        {
            InitializeComponent();
        }

        BookManagementBLL bookManagementBLL = new BookManagementBLL();

        /// <summary>
        /// 窗体加载时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AmendPage_Load(object sender, EventArgs e)
        {
            //赋值输入框
            UpdateBook();
        }

        /// <summary>
        /// 根据图书id查询数据并绑定输入框
        /// </summary>
        private void UpdateBook()
        {
            //赋值给下输入框
            DataTable dt = bookManagementBLL.getBookbyid(bookid);
            txb_BookID.Text = dt.Rows[0]["图书id"].ToString();
            txb_Name.Text = dt.Rows[0]["图书名称"].ToString();
            txb_Author.Text = dt.Rows[0]["作者"].ToString();
            txb_Money.Text = dt.Rows[0]["价格"].ToString();
            //下拉框
            cb_CategoryType.DisplayMember = "CategoryName";
            cb_CategoryType.ValueMember = "ID";
            cb_CategoryType.DataSource = bookManagementBLL.FindCategory();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_AddBook_Click(object sender, EventArgs e)
        {
            //判断用户的值
            if (Judge())
            {
                //修改
                Update();
            }

        }

        /// <summary>
        /// 修改方法实现
        /// </summary>
        private void Update()
        {
            //获取用户输入值 
            int CategoryType = int.Parse(this.cb_CategoryType.SelectedValue.ToString());
            double Money = double.Parse(txb_Money.Text);
            BookInfo book = new Model.BookInfo(txb_Name.Text, CategoryType, txb_Author.Text, Money, txb_BookID.Text);
            book._Id = id;
            //修改
            string updatebook = bookManagementBLL.UpdateBook(book);
            if (updatebook.Equals(EnumTools.GetDescription(ManageEnums.DOCUMENT.Update_SUCCEED)))
            {
                MessageBox.Show(updatebook);
                this.Close();
                DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(updatebook);
            }
        }

        /// <summary>
        /// 判断用户的值
        /// </summary>
        private bool Judge()
        {

            Regex reg = new Regex("^[1-9]\\d*$");
            Regex str = new Regex("^[A-Za-z0-9]+$");
            if (txb_Name.Text == "")
            {
                MessageBox.Show("图书名称不能为空");
                return false;
            }
            else if (!str.Match(txb_BookID.Text).Success)
            {
                MessageBox.Show("请输入正确的图书编号");
                return false;
            }
            else if (this.cb_CategoryType.SelectedValue.ToString() == "")
            {
                MessageBox.Show("请选择图书分类");
                return false;
            }
            else if (txb_Author.Text == "")
            {
                MessageBox.Show("图书作者不能为空");
                return false;
            }
            else if (txb_Money.Text == "")
            {
                MessageBox.Show("请输入价格");
                return false;
            }
            else if (!reg.Match(txb_Money.Text).Success)
            {
                MessageBox.Show("请输入正确的价格");
                return false;
            }
            else if (txb_BookID.Text == "")
            {
                MessageBox.Show("请输入图书id");
                return false;
            }
            return true;
        }
    }
}
